import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

import Home from '../views/layout/Home.vue'
import Login from '../views/layout/Login.vue'
import getMenuRoute from '@/utils/permission'

Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
// push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
const routes = [
  {
    path: '/user-login',
    name: 'userLogin',
    meta: {
      title: '登录',
      hidden: true
    },
    component: Login
  },
  {
    path: '/',
    name: 'Home',
    meta: {
      title: '封面',
      icon: 'home',
      hidden: true
    },
    component: Home
  }
]

const asyncRouterMap = [
  // 1级管理员
  {
    path: '/system',
    name: 'System',
    meta: {
      title: '系统管理',
      hidden: false,
      main: true
    },
    component: Home,
    children: [
      {
        path: 'system-user',
        name: 'SystemUser',
        meta: {
          title: '用户管理',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/systemUser.vue')
      },
      {
        path: 'system-simplify',
        name: 'Simplify',
        meta: {
          title: '报表简略信息',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/simplifyList.vue')
      },
      {
        path: 'system-dictionary',
        name: 'Dictionary',
        meta: {
          title: '学校、学科',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/dictionary.vue')
      }
    ]
  },

  // 2级管理员
  {
    path: '/school',
    name: 'School',
    meta: {
      title: '学校管理员',
      hidden: false,
      main: true
    },
    component: Home,
    children: [
      {
        path: 'school-user',
        name: 'SchoolUser',
        meta: {
          title: '用户管理',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/schoolUser.vue')
      },
      {
        path: 'school-simplify',
        name: 'Simplify',
        meta: {
          title: '报表简略信息',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/simplifyList.vue')
      }
    ]
  },
  // 1-0
  {
    path: '/1-0',
    name: '1-0',
    meta: {
      title: '封面',
      icon: 'inbox',
      hidden: false
    },
    component: Home,
    children: [
      {
        path: '1-0-1',
        name: '1-0-1',
        meta: {
          title: '学科评估简况表',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/1-0-1.vue')
      }
    ]
  },
  // 1-1
  {
    path: '/1-1',
    name: '1-1',
    meta: {
      title: '人才培养质量',
      icon: 'inbox',
      hidden: false
    },
    component: Home,
    children: [
      {
        path: '1-1-1',
        name: '1-1-1',
        meta: {
          title: '思想政治教育特色与成效',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/1-1-1.vue')
      }
    ]
  },
  {
    path: '/1-2',
    name: '1-2',
    meta: {
      title: '出版教材质量',
      icon: 'inbox',
      hidden: false
    },
    component: Home,
    children: [
      {
        path: '1-2-1',
        name: '1-2-1',
        meta: {
          title: '出版教材质量',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/1-2-1.vue')
      }
    ]
  },
  {
    path: '/1-3',
    name: '1-3',
    meta: {
      title: '课程建设与教学质量',
      icon: 'inbox',
      hidden: false
    },
    component: Home,
    children: [
      {
        path: '1-3-1',
        name: '1-3-1',
        meta: {
          title: '研究生课程体系',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/1-3-1.vue')
      },
      {
        path: '1-3-2',
        name: '1-3-2',
        meta: {
          title: '国家级一流课程',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/1-3-2.vue')
      },
      {
        path: '1-3-3',
        name: '1-3-3',
        meta: {
          title: '教学成果奖',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/1-3-3.vue')
      }
    ]
  },
  {
    path: '/1-4',
    name: '1-4',
    meta: {
      title: '学生国际交流情况',
      icon: 'inbox',
      hidden: false
    },
    component: Home,
    children: [
      {
        path: '1-4-1',
        name: '1-4-1',
        meta: {
          title: '学生国际交流情况',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/1-4-1.vue')
      }
    ]
  },
  {
    path: '/1-5',
    name: '1-5',
    meta: {
      title: '在校生代表性成果',
      icon: 'inbox',
      hidden: false
    },
    component: Home,
    children: [
      {
        path: '1-5-1',
        name: '1-5-1',
        meta: {
          title: '在校生代表性成果',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/1-5-1.vue')
      }
    ]
  },
  {
    path: '/1-6',
    name: '1-6',
    meta: {
      title: '学生就业与职业发展质量',
      icon: 'inbox',
      hidden: false
    },
    component: Home,
    children: [
      {
        path: '1-6-1',
        name: '1-6-1',
        meta: {
          title: '学生整体就业情况',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/1-6-1.vue')
      },
      {
        path: '1-6-2',
        name: '1-6-2',
        meta: {
          title: '代表性毕业生',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/1-6-2.vue')
      }
    ]
  },
  {
    path: '/2-1',
    name: '2-1',
    meta: {
      title: '师德师风建设机制与成效',
      icon: 'inbox',
      hidden: false
    },
    component: Home,
    children: [
      {
        path: '2-1-1',
        name: '2-1-1',
        meta: {
          title: '师德师风建设机制与成效',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/2-1-1.vue')
      }
    ]
  },
  {
    path: '/2-2',
    name: '2-2',
    meta: {
      title: '师资队伍建设质量',
      icon: 'inbox',
      hidden: false
    },
    component: Home,
    children: [
      {
        path: '2-2-1',
        name: '2-2-1',
        meta: {
          title: '师资队伍基本情况',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/2-2-1.vue')
      },
      {
        path: '2-2-2',
        name: '2-2-2',
        meta: {
          title: '代表性教师基本情况',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/2-2-2.vue')
      }
    ]
  },
  {
    path: '/2-3',
    name: '2-3',
    meta: {
      title: '支撑平台与重大仪器设备',
      icon: 'inbox',
      hidden: false
    },
    component: Home,
    children: [
      {
        path: '2-3-1',
        name: '2-3-1',
        meta: {
          title: '支撑平台',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/2-3-1.vue')
      },
      {
        path: '2-3-2',
        name: '2-3-2',
        meta: {
          title: '重大仪器设备',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/2-3-2.vue')
      }
    ]
  },
  {
    path: '/3-1',
    name: '3-1',
    meta: {
      title: '学术论文质量',
      icon: 'inbox',
      hidden: false
    },
    component: Home,
    children: [
      {
        path: '3-1-1',
        name: '3-1-1',
        meta: {
          title: '学术论文质量',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/3-1-1.vue')
      }
    ]
  },
  {
    path: '/3-2',
    name: '3-2',
    meta: {
      title: '专利转化情况',
      icon: 'inbox',
      hidden: false
    },
    component: Home,
    children: [
      {
        path: '3-2-1',
        name: '3-2-1',
        meta: {
          title: '专利转化情况',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/3-2-1.vue')
      }
    ]
  },
  {
    path: '/3-3',
    name: '3-3',
    meta: {
      title: '科研项目情况',
      icon: 'inbox',
      hidden: false
    },
    component: Home,
    children: [
      {
        path: '3-3-1',
        name: '3-3-1',
        meta: {
          title: '国家级科研项目',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/3-3-1.vue')
      },
      {
        path: '3-3-2',
        name: '3-3-2',
        meta: {
          title: '重要军队国防项目',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/3-3-2.vue')
      },
      {
        path: '3-3-3',
        name: '3-3-3',
        meta: {
          title: '其他代表性科研项目',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/3-3-3.vue')
      }
    ]
  },
  {
    path: '/3-4',
    name: '3-4',
    meta: {
      title: '科研获奖情况',
      icon: 'inbox',
      hidden: false
    },
    component: Home,
    children: [
      {
        path: '3-4-1',
        name: '3-4-1',
        meta: {
          title: '科研获奖情况',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/3-4-1.vue')
      }
    ]
  },
  {
    path: '/4-1',
    name: '4-1',
    meta: {
      title: '社会服务贡献',
      icon: 'inbox',
      hidden: false
    },
    component: Home,
    children: [
      {
        path: '4-1-1',
        name: '4-1-1',
        meta: {
          title: '社会服务贡献',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/4-1-1.vue')
      }
    ]
  },
  {
    path: '/4-2',
    name: '4-2',
    meta: {
      title: '学科简介',
      icon: 'inbox',
      hidden: false
    },
    component: Home,
    children: [
      {
        path: '4-2-1',
        name: '4-2-1',
        meta: {
          title: '学科简介',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/4-2-1.vue')
      }
    ]
  },
  {
    path: '/4-3',
    name: '4-3',
    meta: {
      title: ' Introduction to the Discipline',
      icon: 'inbox',
      hidden: false
    },
    component: Home,
    children: [
      {
        path: '4-3-1',
        name: '4-3-1',
        meta: {
          title: ' Introduction to the Discipline',
          icon: 'unordered-list',
          hidden: false
        },
        component: () => import('@/views/page/4-3-1.vue')
      }
    ]
  }

]

const router = new VueRouter({
  routes,
  // 解决vue页面之间跳转，页面不是在顶部的问题
  scrollBehavior (to, from, savedPosition) {
    return { x: 0, y: 0 }
  }
})

let isAddRoutes = false
router.beforeEach((to, from, next) => {
  NProgress.start()
  if (to.path !== '/user-login') {
    if (store.state.user.username && store.state.user.role) {
      if (!isAddRoutes) {
        const menuRoutes = getMenuRoute(store.state.user.role, asyncRouterMap)
        store.dispatch('changeMenuRoutes', routes.concat(menuRoutes)).then(() => {
          for (const x of menuRoutes) {
            router.addRoute(x)
          }
          next()
        })
        isAddRoutes = true
      }
      return next()
    }
    isAddRoutes = false
    return next('/user-login')
  } else {
    if (store.state.user.username && store.state.user.role) {
      NProgress.done()
      return next(from.path)
    }
  }
  return next()
})
router.afterEach(() => {
  NProgress.done()
})
export default router
